import socket
import time
import uuid
import random
import uart.honglieUart as hlu
from ctypes import *
#set,mamawhes,controller,robot
#add,1,2,3,4,5,6
#第一次发送的值会赋值给Player
#之后发送的值会赋值给Robot
def buffer_to_bytes(buffer:Array[c_uint8]):
    temp_list=[]
    for b in buffer:
        temp_list.append(b)
    return bytes(temp_list)
def recv_to_buffer(recv:bytes):
    pass
    



playerid:str=str(uuid.uuid1())[:8]
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # 创建客户端Socket
s.connect(('127.0.0.1', 25565))  # 连接客户端Socket到服务器ip和端口
x=0
while(True):
    
    buffer=hlu.get_bytes(hlu.Element(c_uint32,x,None))
    s.send(buffer)
    
    restemp=s.recv(1024)
    #print(restemp.decode(errors="replace"))
    if(restemp!='NULL'.encode()):
        buffers=restemp.split("OVER".encode())
        for buf in buffers:
            print(buf.decode(errors="replace"))
    # if(restemp!='null'.encode()):
    #     commands=restemp.split(hlu.get_buffer(hlu.Element(c_uint8,127)))
    #     if(len(commands)!=0):
    #         for command in commands:
    #             temp=command.split(hlu.get_buffer(hlu.Element(c_uint8,128)))
    #             if(len(temp)>1):
    #                 elements=hlu.get_elements(buffer_bytes=temp[1])
    #                 print(temp[0].decode(),elements)
    time.sleep(random.random()*1)
    x+=1
s.close()

